package com.ruoyi.ctcx.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ctcx.domain.TbInsuranceAttachment;
import com.ruoyi.ctcx.domain.TbInsuranceAttachmentRelate;
import com.ruoyi.ctcx.domain.ro.InsuranceParam;
import com.ruoyi.ctcx.domain.vo.TbInsuranceAllVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceMainSaveVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceMainVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceXlMainVo;
import com.ruoyi.ctcx.service.ITbInsuranceAttachmentRelateService;
import com.ruoyi.ctcx.service.ITbInsuranceAttachmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ctcx.domain.TbInsuranceMain;
import com.ruoyi.ctcx.service.ITbInsuranceMainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险主Controller
 * 
 * @author ruoyi
 * @date 2024-11-19
 */
@RestController
@RequestMapping("/insurance/main")
public class TbInsuranceMainController extends BaseControllerExt
{
    @Autowired
    private ITbInsuranceMainService tbInsuranceMainService;


    @Autowired
    private ITbInsuranceAttachmentService tbInsuranceAttachmentService;



    /**
     * 查询保险主列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:main:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        startPage();
        List<TbInsuranceMainVo> list = tbInsuranceMainService.selectTbInsuranceMainList(insuranceParam);
        return getDataTable(list);
    }

    /**
     * 查询保险主列表
     */
    @GetMapping("/getMainList")
    public TableDataInfo getMainList(InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        startPage();
        List<TbInsuranceAllVo> list = tbInsuranceMainService.getMainList(insuranceParam);
        return getDataTable(list);
    }

    /**
     * 查询保险下拉框
     */
    @GetMapping("/getXlMainList")
    public AjaxResult getXlMainList(InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        List<TbInsuranceXlMainVo> list = tbInsuranceMainService.getXlMainList(insuranceParam);
        return success(list);
    }

    /**
     * 导出保险主列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:main:export')")
    @Log(title = "保险主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        List<TbInsuranceMainVo> list = tbInsuranceMainService.selectTbInsuranceMainList(insuranceParam);
        ExcelUtil<TbInsuranceMainVo> util = new ExcelUtil<TbInsuranceMainVo>(TbInsuranceMainVo.class);
        util.exportExcel(response, list, "保险主数据");
    }

    /**
     * 获取保险主详细信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        //主信息
        TbInsuranceMainVo tbInsuranceMainVo = tbInsuranceMainService.selectTbInsuranceMainById(id);

        List<TbInsuranceAttachment> tbInsuranceAttachmentList = tbInsuranceAttachmentService.selectTbInsuranceAttachmentByMainId(id);

        if (null != tbInsuranceAttachmentList && !tbInsuranceAttachmentList.isEmpty())
        {
            tbInsuranceMainVo.setTbInsuranceAttachmentList(tbInsuranceAttachmentList);
        }
        return success(tbInsuranceMainVo);
    }

    /**
     * 新增保险主数据
     */
    @PreAuthorize("@ss.hasPermi('insurance:main:add')")
    @Log(title = "新增保险主数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceMainSaveVo tbInsuranceMainSaveVo)
    {
        //保存保险主表
        TbInsuranceMain tbInsuranceMain = new TbInsuranceMain();
        BeanUtils.copyProperties(tbInsuranceMainSaveVo, tbInsuranceMain);
        tbInsuranceMain.setCreateBy(getUsername());
        tbInsuranceMainService.insertTbInsuranceMain(tbInsuranceMain);

        //保存附件信息
        tbInsuranceAttachmentService.saveAttachment(tbInsuranceMain.getId().toString(),tbInsuranceMainSaveVo.getTbInsuranceAttachmentList(),getUsername());
        return success(1);
    }

    /**
     * 修改保险主
     */
    @PreAuthorize("@ss.hasPermi('insurance:main:edit')")
    @Log(title = "保险主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceMainSaveVo tbInsuranceMainSaveVo)
    {
        //保存保险主表
        TbInsuranceMain tbInsuranceMain = new TbInsuranceMain();
        BeanUtils.copyProperties(tbInsuranceMainSaveVo, tbInsuranceMain);
        tbInsuranceMain.setUpdateBy(getUsername());
        tbInsuranceMainService.updateTbInsuranceMain(tbInsuranceMain);

        //保存附件信息
        tbInsuranceAttachmentService.saveAttachment(tbInsuranceMain.getId().toString(),tbInsuranceMainSaveVo.getTbInsuranceAttachmentList(),getUsername());
        return toAjax(1);
    }

    /**
     * 删除保险主
     */
    @PreAuthorize("@ss.hasPermi('insurance:main:remove')")
    @Log(title = "保险主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbInsuranceMainService.deleteTbInsuranceMainByIds(ids));
    }


}
